package br.com.erpnasajon.controle;

import br.com.erpnasajon.dao.ClienteDao;
import br.com.erpnasajon.dao.ClienteDaoImp;
import br.com.erpnasajon.modelo.Cliente;
import br.com.erpnasajon.view.LazyClienteDataModel;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

/**
 *
 * @author MENDONCA
 */
@ManagedBean
@SessionScoped
public class ClienteControle {

    private Cliente cliente;
    private LazyClienteDataModel listaClientes;
    private ClienteDao dao;
    private boolean incluindo = false;

    public ClienteControle() {
        dao = new ClienteDaoImp();

        listaClientes = new LazyClienteDataModel(dao);
    }

    public void prepararAdicionarCliente(ActionEvent actionEvent) {
        this.incluindo = true;
        this.cliente = new Cliente();
    }

    public void prepararAlterarCliente(ActionEvent actionEvent) {
        this.incluindo = false;
    }

    public void prepararExcluirCliente(ActionEvent actionEvent) {
        this.incluindo = false;
    }

    public void adicionarCliente(ActionEvent actionEvent) {
        this.dao.save(this.cliente);
    }

    public void alterarCliente(ActionEvent actionEvent) {
        this.dao.update(this.cliente);
    }

    public void excluirCliente(ActionEvent actionEvent) {
        this.dao.remove(this.cliente);
        this.cliente = null;
    }

    public Cliente getCliente() {
        return cliente;
    }

    public LazyClienteDataModel getListaClientes() {
        return this.listaClientes;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public boolean isIncluindo() {
        return incluindo;
    }

    public boolean isSelected() {
        return (this.cliente != null);
    }
}
